package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.MyCollectionHolder;
import where2go.sctu.com.zhiyou.db.MyPurchareHoder;
import where2go.sctu.com.zhiyou.entity.CollectionData;
import where2go.sctu.com.zhiyou.entity.PurchareData;
import where2go.sctu.com.zhiyou.entity.User;

/**
 * Created by lenovo on 2018/5/27.
 */

public class PurchareAdapter extends RecyclerView.Adapter<MyPurchareHoder> {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private String url= "http://47.104.80.155/youyiyou/resources/img/";
    private Context context;
    private List<PurchareData.DataBean.AClassBean> dataList1 = new ArrayList<PurchareData.DataBean.AClassBean>();
    private List<User> userList = new ArrayList<User>();
    public PurchareAdapter(Context context, List<CollectionData.DataBean.AClassBean> dataList) {
        this.context = context;
        this.dataList1 = dataList1;
    }
    @NonNull
    @Override
    public MyPurchareHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_purchare, parent, false);
        MyPurchareHoder holder = new MyPurchareHoder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyPurchareHoder holder, int position) {
        PurchareData.DataBean.AClassBean classData=dataList1.get(position);
        holder.name.setText(classData.getClassTitle());
        holder.content.setText(classData.getClassDetail());
        String s=format.format(new Date(Long.parseLong(classData.getClassTime())));
        holder.time.setText(s);
        holder.price.setText(classData.getClassPrice());
        ImageRequest imageRequest = new ImageRequest(url + dataList1.get(position),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        holder.image.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片

                holder.image.setImageResource(R.mipmap.ic_launcher);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);
    }

    @Override
    public int getItemCount() {
        return dataList1 != null ? dataList1.size() : 0;
    }

    public void addList(List<PurchareData.DataBean.AClassBean> classList) {
        dataList1.addAll(classList);
    }
}
